package cn.juque.systemservice.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseOperator;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.thread.BaseOperatorThreadLocal;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.systemservice.api.constant.SystemServiceMsgEnum;
import cn.juque.systemservice.api.dto.employee.*;
import cn.juque.systemservice.api.dto.org.OrgRelationDTO;
import cn.juque.systemservice.api.dto.role.RoleRelationDTO;
import cn.juque.systemservice.api.provider.IEmployeeInfoProvider;
import cn.juque.systemservice.bo.employee.EmployeeInfoAddBO;
import cn.juque.systemservice.bo.employee.EmployeeInfoDetailBO;
import cn.juque.systemservice.bo.employee.EmployeeInfoUpdateBO;
import cn.juque.systemservice.bo.employee.EmployeePasswordBO;
import cn.juque.systemservice.entity.EmployeeInfo;
import cn.juque.systemservice.service.IEmployeeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>员工信息</li>
 * </ul>
 * @date 2022-11-03 12:12:51
 **/
@Slf4j
@Service("employeeInfoProvider")
@HandlerAppException
public class EmployeeInfoProviderImpl implements IEmployeeInfoProvider {

    @Resource
    private BaseOperatorThreadLocal baseOperatorThreadLocal;

    @Resource
    private IEmployeeInfoService employeeInfoService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDTO 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<EmployeeInfoListDTO>> list(EmployeeInfoSearchDTO searchDTO) {
        this.validatorUtil.checkNotNull(searchDTO);
        PageInfo pageInfo = searchDTO.getPageInfo();
        EmployeeInfo employeeInfo = new EmployeeInfo();
        BeanUtil.copyProperties(searchDTO, employeeInfo);
        List<EmployeeInfo> infoList;
        if (null == pageInfo) {
            infoList = this.employeeInfoService.listInfoWithoutPage(employeeInfo);
        } else {
            infoList = this.employeeInfoService.listInfoWithPage(pageInfo, employeeInfo);
        }
        List<EmployeeInfoListDTO> infoDtoList = BeanUtil.copyToList(infoList, EmployeeInfoListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, infoDtoList, pageInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return EmployeeInfoDetailDTO
     */
    @Override
    public BaseResponseDTO<EmployeeInfoDetailDTO> detail(String id) {
        EmployeeInfoDetailBO detail = this.employeeInfoService.detail(id);
        List<OrgRelationDTO> orgRelationList = BeanUtil.copyToList(detail.getOrgList(), OrgRelationDTO.class);
        List<RoleRelationDTO> roleRelationList = BeanUtil.copyToList(detail.getRoleList(), RoleRelationDTO.class);
        EmployeeInfoDetailDTO detailDTO = new EmployeeInfoDetailDTO();
        BeanUtil.copyProperties(detail, detailDTO);
        detailDTO.setOrgList(orgRelationList);
        detailDTO.setRoleList(roleRelationList);
        return new BaseResponseDTO<>(MessageEnum.OK, detailDTO);
    }

    /**
     * 操作人详情
     *
     * @param requestDTO 操作信息
     * @return EmployeeInfoDetailDTO
     */
    @Override
    public BaseResponseDTO<EmployeeInfoDetailDTO> getByOperator(EmployeeCurrentParamDTO requestDTO) {
        BaseOperator operator = requestDTO.getBaseOperator();
        if (null == operator) {
            return new BaseResponseDTO<>(SystemServiceMsgEnum.USER_MISSING.getCode(), SystemServiceMsgEnum.USER_MISSING.getMsg());
        }
        return this.detail(operator.getOperatorId());
    }

    /**
     * 获取员工密码
     *
     * @param employeePasswordParamDTO 入参
     * @return EmployeeInfoDetailDTO
     */
    @Override
    public BaseResponseDTO<EmployeePasswordResultDTO> getPassword(EmployeePasswordParamDTO employeePasswordParamDTO) {
        this.validatorUtil.checkNotNull(employeePasswordParamDTO);
        EmployeePasswordBO employeePasswordBO = new EmployeePasswordBO();
        BeanUtil.copyProperties(employeePasswordParamDTO, employeePasswordBO);
        EmployeeInfo employeeInfo = this.employeeInfoService.getPassword(employeePasswordBO);
        if (null == employeeInfo) {
            return new BaseResponseDTO<>(MessageEnum.OK, null);
        }
        EmployeePasswordResultDTO employeePasswordResultDTO = new EmployeePasswordResultDTO();
        employeePasswordResultDTO.setEmployeeId(employeeInfo.getId());
        employeePasswordResultDTO.setPassword(employeeInfo.getEmployeePassword());
        return new BaseResponseDTO<>(MessageEnum.OK, employeePasswordResultDTO);
    }

    /**
     * 获取员工信息
     *
     * @param req 入参
     * @return list
     */
    @Override
    public BaseResponseDTO<List<EmployeeInfoDetailDTO>> getByUserId(EmployeeInfoSearchByUserIdDTO req) {
        if (CollUtil.isEmpty(req.getUserIdList())) {
            return new BaseResponseDTO<>(MessageEnum.OK, CollUtil.newArrayList());
        }
        List<EmployeeInfoDetailBO> detailList = this.employeeInfoService.getByUserId(req.getUserIdList());
        List<EmployeeInfoDetailDTO> dtoList = BeanUtil.copyToList(detailList, EmployeeInfoDetailDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, dtoList);
    }

    /**
     * 获取员工信息
     *
     * @param req 入参
     * @return list
     */
    @Override
    public BaseResponseDTO<List<EmployeeInfoDetailDTO>> getByIdList(EmployeeInfoSearchByIdDTO req) {
        if (CollUtil.isEmpty(req.getIdList())) {
            return new BaseResponseDTO<>(MessageEnum.OK, CollUtil.newArrayList());
        }
        List<EmployeeInfoDetailBO> boList = req.getIdList().stream().map(t -> this.employeeInfoService.detail(t)).collect(
                Collectors.toList());
        List<EmployeeInfoDetailDTO> dtoList = BeanUtil.copyToList(boList, EmployeeInfoDetailDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, dtoList);
    }

    /**
     * 获取员工信息
     *
     * @param req 入参
     * @return list
     */
    @Override
    public BaseResponseDTO<List<EmployeeInfoDetailDTO>> getByRoleIdList(EmployeeInfoSearchByRoleDTO req) {
        this.validatorUtil.fastValidate(req);
        List<String> roleIdList = req.getRoleIdList();
        List<EmployeeInfoDetailBO> boList = this.employeeInfoService.getByRoleId(roleIdList);
        List<EmployeeInfoDetailDTO> dtoList = BeanUtil.copyToList(boList, EmployeeInfoDetailDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, dtoList);
    }

    /**
     * 新增
     *
     * @param addDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> add(EmployeeInfoAddDTO addDTO) {
        this.validatorUtil.fastValidate(addDTO);
        this.baseOperatorThreadLocal.set(addDTO.getBaseOperator());
        EmployeeInfoAddBO employeeInfoAddBO = new EmployeeInfoAddBO();
        BeanUtil.copyProperties(addDTO, employeeInfoAddBO);
        this.employeeInfoService.saveInfo(employeeInfoAddBO);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 更新
     *
     * @param updateDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> update(EmployeeInfoUpdateDTO updateDTO) {
        this.validatorUtil.fastValidate(updateDTO);
        this.baseOperatorThreadLocal.set(updateDTO.getBaseOperator());
        EmployeeInfoUpdateBO employeeInfoUpdateBO = new EmployeeInfoUpdateBO();
        BeanUtil.copyProperties(updateDTO, employeeInfoUpdateBO);
        this.employeeInfoService.updateInfo(employeeInfoUpdateBO);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 更新密码
     *
     * @param employeeUpdatePasswordDTO UpdatePasswordDTO
     */
    @Override
    public BaseResponseDTO<Boolean> updatePassword(EmployeeUpdatePasswordDTO employeeUpdatePasswordDTO) {
        this.validatorUtil.fastValidate(employeeUpdatePasswordDTO);
        this.baseOperatorThreadLocal.set(employeeUpdatePasswordDTO.getBaseOperator());
        this.employeeInfoService.updatePassword(
                employeeUpdatePasswordDTO.getEmployeeId(),
                employeeUpdatePasswordDTO.getPassword(),
                employeeUpdatePasswordDTO.getNewPassword(),
                employeeUpdatePasswordDTO.getConfirmPassword());
        return new BaseResponseDTO<>(MessageEnum.OK, true);
    }

    /**
     * 删除
     *
     * @param req 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> delete(EmployeeInfoDeleteDTO req) {
        List<String> infoIdList = req.getIdList();
        this.employeeInfoService.deleteInfo(infoIdList);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }
}
